<template>
<div class="user">
    <!--添加等级-->
    <div class="common-level-rail">
      <el-button
        size="small"
        type="primary"
        @click="addClick"
        icon="Plus"
        v-auth="'/plus/fullreduce/add'"
        >{{$t('plus.add_activity')}}</el-button
      >
    </div>

    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table
          size="small"
          :data="tableData"
          border
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column prop="activeName" :label="$t('plus.activity_name')"></el-table-column>
          <el-table-column prop="fullType" :label="$t('plus.threshold_type')">
            <template #default="scope">
              <span v-if="scope.row.fullType == 1">{{$t('plus.full_amount')}}</span>
              <span v-if="scope.row.fullType == 2">{{$t('plus.threshold_quantity')}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="fullValue" :label="$t('plus.threshold_value')"></el-table-column>
          <el-table-column prop="reduceType" :label="$t('plus.discount_type')">
            <template #default="scope">
              <span v-if="scope.row.reduceType == 1">{{$t('plus.discount_amount')}}</span>
              <span v-if="scope.row.reduceType == 2">{{$t('plus.discount_percent')}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="reduceValue" :label="$t('plus.discount_value')"></el-table-column>
          <el-table-column fixed="right" :label="$t('plus.action')" width="90">
            <template #default="scope">
              <el-button
                @click="editClick(scope.row)"
                type="text"
                size="small"
                v-auth="'/plus/fullreduce/edit'"
                >{{$t('plus.edit')}}</el-button
              >
              <el-button
                @click="deleteClick(scope.row)"
                type="text"
                size="small"
                v-auth="'/plus/fullreduce/delete'"
                >{{$t('plus.delete')}}</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!--分页-->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="curPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="totalDataNumber"
        >
        </el-pagination>
      </div>
    </div>

    <!--添加-->
    <Add
      v-if="open_add"
      :open_add="open_add"
      @closeDialog="closeDialogFunc($event, 'add')"
    ></Add>

    <!--编辑-->
    <Edit
      v-if="open_edit"
      :open_edit="open_edit"
      :form="currentModel"
      @closeDialog="closeDialogFunc($event, 'edit')"
    ></Edit>
  </div>
</template>

<script>
import FullreduceApi from "@/api/plus/fullreduce.js";
import Edit from "./Edit.vue";
import Add from "./Add.vue";
import { deepClone } from "@/utils/base.js";
export default {
  components: {
    /*编辑组件*/
    Edit,
    Add
  },
  data() {
    return {
      /*是否加载完成*/
      loading: true,
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 20,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*横向表单数据模型*/
      formInline: {
        user: "",
        region: ""
      },
      /*是否打开添加弹窗*/
      open_add: false,
      /*是否打开编辑弹窗*/
      open_edit: false,
      /*当前编辑的对象*/
      currentModel: {}
    };
  },
  created() {
    /*获取列表*/
    this.getTableList();
  },
  methods: {
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getTableList();
    },
    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getTableList();
    },
    /*获取列表*/
    getTableList() {
      let self = this;
      let Params = {};
      Params.pageIndex = self.curPage;
      Params.pageSize = self.pageSize;
      FullreduceApi.getList(Params, true).then(res => {
        self.loading = false;
        self.tableData = res.data.records;
        self.totalDataNumber = res.data.total;
      }).catch(error => {});
    },
    /*打开添加*/
    addClick() {
      this.open_add = true;
    },
    /*打开编辑*/
    editClick(item) {
      this.currentModel = deepClone(item);
      this.open_edit = true;
    },
    /*关闭弹窗*/
    closeDialogFunc(e, f) {
      if (f == "add") {
        this.open_add = e.openDialog;
        if (e.type == "success") {
          this.getTableList();
        }
      }
      if (f == "edit") {
        this.open_edit = e.openDialog;
        if (e.type == "success") {
          this.getTableList();
        }
      }
    },
    /*删除用户*/
    deleteClick(row) {
      let self = this;
      ElMessageBox.confirm($t("plus.delete_confirm"), $t("plus.prompt"), {
        confirmButtonText: $t("plus.confirm_text"),
        cancelButtonText: $t("plus.cancel_text"),
        type: "warning"
      }).then(() => {
        self.loading = true;
        FullreduceApi.del({
          fullreduceId: row.fullreduceId
        }, true).then(data => {
          self.loading = false;
          ElMessage({
            message: data.msg,
            type: "success"
          });
          self.getTableList();
        }).catch(error => {
          self.loading = false;
        });
      }).catch(() => {});
    }
  }
};
</script>

